package com.machine.filling.function.main.erweima;

import android.content.Context;

import com.google.gson.Gson;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.modle.ClientInfoModle;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.ws.req.WsGetErWeiMaReq;
import com.machine.filling.net.ws.response.WsResCheckOrderPayState;
import com.machine.filling.net.ws.response.WsResGetErWeiMa;
import com.machine.filling.util.Common;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/4/19 0019.
 */

public class ErWeiMaPresenter implements ErWeiMaContract.Presenter {
    private Context mContext;
    private ErWeiMaContract.View mView;
    private boolean isStopCycle;

    public ErWeiMaPresenter(Context mContext, ErWeiMaContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        stopCycle(false);
    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {

    }

    @Override
    @Deprecated
    public void getErWeiMa() {

        ClientInfoModle clientInfoModle = ClientInfoDao.getInstance().getModleByDeviceNum();
        String token = clientInfoModle.getToken();
        String sellerId = clientInfoModle.getStationId();
        String machineId = clientInfoModle.getMachineId();
        String orderNum = mView.getOrderNum();
        APIHttpManager.getInstance().getErWeiMa(mView.getPayType(),token,orderNum, sellerId, machineId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                WsResGetErWeiMa bean = new Gson().fromJson(result,WsResGetErWeiMa.class);
                mView.showGetErWeiMaResultSuccess(bean);
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                mView.showGetErWeiMaResultFailed(errorCode,errorMsg);
            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void getCyclePayState() {
        if (isStopCycle){
            return;
        }
        ClientInfoModle clientInfoModle = ClientInfoDao.getInstance().getModleByDeviceNum();
        String token = clientInfoModle.getToken();
        String machineId = clientInfoModle.getMachineId();
        String orderNum = mView.getOrderNum();
        APIHttpManager.getInstance().getOrderPayState(token, machineId, orderNum, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                WsResCheckOrderPayState bean = new Gson().fromJson(result,WsResCheckOrderPayState.class);
                //1已付款 2未付款 3已取消
                if ("1".equals(bean.getState())){
                    mView.showPayResSuccess(bean);
                    isStopCycle = true;
                }else {
                    isStopCycle = false;
                    getCyclePayState();
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                getCyclePayState();
            }

            @Override
            public void onFinished() {
//                getCyclePayState();
            }
        });
    }

    @Override
    public void stopCycle(boolean isStop) {
        this.isStopCycle = isStop;
    }

}
